/*
list: 要查找的列表
path: 查找路径
jud: 是否删除查找对象--boolean
*/
interface Tree {
  tagName: string,
  attrs: {
    draggable: string,
    class: string,
    "data-type": string,
    [organName: string]: string
  },
  style: {
    [organName: string]: string
  },
  slot: string,
  goto: { type: string, value: string },
  context: { type: string, value: string },
  children: Tree[]
  faPosition?: []
  on: {
    click: Function,
    [organName: string]: Function
  }
}

interface Path {
  className: string
}

function searchDom(list: Tree[], path: Array<Path>, jud: boolean) {
  // console.log(list, path, jud);
  if (jud != true && path.length == 1 && path[0].className.includes("app")) {
    //好像没有用
    //传入了页面的根标签，不用查找
    return list;
  }

  for (let ind_1 = 0; ind_1 < path.length; ind_1++) {
    //有空class名跳过
    if (path[ind_1].className == "") continue;

    for (let ind_2 = 0; ind_2 < list.length; ind_2++) {
      //如果该节点的className不相符，跳过本次操作--子节点也不用看了，直接看兄弟节点即可
      if (!list[ind_2].attrs.class.includes(path[ind_1].className)) {
        if (
          !list[ind_2].attrs.class.includes(
            path[ind_1].className.replace(" is-opened", "")
          )
        )
          //防止一些干扰
          continue;
      }

      //如果是最后一个className,是查找路径的末端了，没有下一个了
      if (ind_1 == path.length - 1) {
        //删除dom节点--无返回
        if (jud) return list.splice(ind_2, 1)[0];
        //返回dom节点
        return list[ind_2];
      } else {
        // 传入子节点继续遍历
        list = list[ind_2].children;
        break;
      }
    }
  }
}
export default searchDom;
